package cn.wetcland.urban.admin.service.impl;

import cn.wetcland.urban.admin.dto.DriverUpdateDTO;
import cn.wetcland.urban.admin.mapper.UtDriverMapper;
import cn.wetcland.urban.admin.service.UtDriverService;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.bcrypt.BCryptPasswordEncoder;
import cn.wetcland.urban.generator.domain.UtCustomer;
import cn.wetcland.urban.generator.domain.UtDriver;
import cn.wetcland.urban.generator.dto.BaseConditionSearchDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Wetcland
* @description 针对表【ut_driver】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtDriverServiceImpl extends ServiceImpl<UtDriverMapper, UtDriver> implements UtDriverService {

    @Resource
    private UtDriverMapper driverMapper;

    @Override
    public Page<UtDriver> driverList(BaseConditionSearchDTO searchDTO) {
        Page<UtDriver> driverPage = new Page<>(searchDTO.getCurPage(), searchDTO.getPageSize());
        return driverMapper.selectDriverListPage(driverPage, searchDTO);
    }

    @Override
    public Page<UtDriver> verifyDriverList(BaseConditionSearchDTO searchDTO) {
        Page<UtDriver> driverPage = new Page<>(searchDTO.getCurPage(), searchDTO.getPageSize());
        return driverMapper.selectVerifyDriverList(driverPage, searchDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleDriverBanOrUnban(Long driverId) {
        UtDriver driver = driverMapper.selectById(driverId);
        MyAssert.notNull(driver, "该司机不存在");
        driver.setIsBan(driver.getIsBan()==1?0:1);
        driverMapper.updateById(driver);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleDriverDelete(Long driverId) {
        driverMapper.deleteById(driverId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void verifyDriver(Long driverId) {
        UtDriver driver = driverMapper.selectById(driverId);
        MyAssert.notNull(driver, "该司机不存在");
        driver.setIsPassVerify(1);
        driverMapper.updateById(driver);
    }

    @Override
    public void handleDriverUpdate(DriverUpdateDTO updateDTO) {
        UtDriver driver = driverMapper.selectById(updateDTO.getDriverId());
        MyAssert.notNull(driver, "该司机不存在");
        driver.setDriverPhone(updateDTO.getPhone());
        if (updateDTO.getPassword() != null && !updateDTO.getPassword().isEmpty()) {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(updateDTO.getPassword());
            driver.setDriverPassword(encode);
        }
        driverMapper.updateById(driver);
    }
}




